package com.zulucap.riftcalc;

import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.zulucap.R;

public class BuildActivity extends BaseActivity {
	int mPointsSpent[] = new int[3];
	Integer mTotalPointsSpent = 0;
	String mSoulString[] = new String[3];
	String mSoul[] = new String[3];
	String mSoulButton = "";
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.build);
        
     // Set custom font    
        TextView tv=(TextView)findViewById(R.id.text_calling);
        Button soul1 = (Button) findViewById(R.id.button_soul1);
        Button soul2 = (Button) findViewById(R.id.button_soul2);
        Button soul3 = (Button) findViewById(R.id.button_soul3);
    	 
        face=Typeface.createFromAsset(getAssets(), "fonts/CrimsonText-Roman.ttf"); 
    	faceBold = Typeface.createFromAsset(getAssets(), "fonts/CrimsonText-Bold.ttf");
    	
    	tv.setTypeface(faceBold);
    	soul1.setTypeface(face);
    	soul2.setTypeface(face);
    	soul3.setTypeface(face);
    	
    	String calling = getIntent().getExtras().getString("calling");
    	
    	tv.setText(calling);
    }
    
    public void addSoul(View view){
    	Intent i;
    	Integer btn_idx = 0;
    	Boolean newSoul = true;
    	
    	TextView tv = (TextView) findViewById(R.id.text_calling);
    	Button thisButton = (Button) findViewById(view.getId());
    	
    	for(Integer j=0; j<3; j++){
    		if(mSoul[j] == thisButton.getText()){
    			newSoul = false;
    		}
    	}

    	// Load Add Soul Activity for new soul
    	if(newSoul){
    		i = new Intent(getApplicationContext(), AddSoulActivity.class);
            i.putExtra("newSoul", true);	
    	// Jump to Soul Tree for existing soul
    	} else{
    		i = new Intent(getApplicationContext(), SoulTreeActivity.class);
    		i.putExtra("newSoul", false);
    	}
        
        if(view.getId() == R.id.button_soul1){
        	btn_idx = 0;
    	}
    	if(view.getId() == R.id.button_soul2){
    		btn_idx = 1;
    	}
    	if(view.getId() == R.id.button_soul3){
    		btn_idx = 2;
    	}
    	
    	i.putExtra("calling", tv.getText());
        i.putExtra("button", view.getId());
        i.putExtra("totalPointsSpent", mTotalPointsSpent);
		i.putExtra("pointsSpent", mPointsSpent[btn_idx]);
        i.putExtra("soulString", mSoulString[btn_idx]);
        i.putExtra("soul", mSoul[btn_idx]);
        i.putExtra("soulPointsSpent", mPointsSpent);
        i.putExtra("souls", mSoul);
                
        startActivityForResult(i, 0);
        
    	/*
    	Context context = getApplicationContext();
    	CharSequence text = "Add Soul!";
    	int duration = Toast.LENGTH_SHORT;

    	Toast toast = Toast.makeText(context, text, duration);
    	toast.show();
    	*/
    }
    
    protected void onActivityResult(int requestCode, int resultCode,
            Intent data) {
        if (requestCode == 0) {
            if (resultCode == RESULT_OK) {
            	Integer btn_idx = 0;
            	
            	mSoulButton = data.getExtras().get("button").toString();
            	
            	Integer intButton = Integer.decode(mSoulButton);
            	Button soulButton = (Button) findViewById(intButton);
            	
            	if(intButton == R.id.button_soul1){
	            	btn_idx = 0;
            	}
            	if(intButton == R.id.button_soul2){
            		btn_idx = 1;
            	}
            	if(intButton == R.id.button_soul3){
            		btn_idx = 2;
            	}
            	
            	
            	mSoul[btn_idx] = (String) data.getExtras().get("soul");
            	soulButton.setText(mSoul[btn_idx]);
            	
            	mSoulString[btn_idx] = (String) data.getExtras().get("soulString");
            	mPointsSpent[btn_idx] = data.getExtras().getInt("pointsSpent");
            	mTotalPointsSpent = data.getExtras().getInt("totalPointsSpent");
            	
            }
        }
    }
}
